package com.xzz.cloud.consumer.order.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.xzz.cloud.consumer.order.service.PaymentHystrixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author: hhz
 * @createDate: 2021-4-2
 */
@RestController
@Slf4j
//@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderHystrixController {
    @Resource
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/consumer/hystrix/ok/{id}")
    public String paymentInfo_ok(@PathVariable(value = "id") Integer id){
        String result= paymentHystrixService.paymentInfo_ok(id);
        return result;
    }

    @GetMapping("/consumer/hystrix/timeout/{id}")
//    @HystrixCommand(fallbackMethod = "paymentTimeOutFallBackMethod",commandProperties = {
//            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="2000")
//    })
    @HystrixCommand
    //这里设置的时间是1.5s,就是说调用服务会等待1.5s,如果超过了就会走fallbackMethod方法
    //而我们在支付的微服务中,时间是3s
    public String paymentInfo_timeout(@PathVariable(value = "id") Integer id){
        String result= paymentHystrixService.paymentInfo_timeout(id);
        return result;
    }

    public String paymentTimeOutFallBackMethod(@PathVariable(value = "id") Integer id){
        return "我是消费者80,支付系统繁忙,请稍后重试 o(╥﹏╥)o";
    }
    //下面是全局fallback方法
    public String payment_Global_FallbackMethod(){
        return "Global异常处理信息,请稍后再试！！";
    }
}
